﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BCCommon;
using BCEntity.WeChatComponent.WeChatMessagePushRecord;
using Dapper;

namespace BCData.WeChatComponent.WeChatMessagePushRecord
{
    /// <summary>
    /// 微信群发记录数据处理
    /// </summary>
    public class WeChatMessagePushRecordData : IWeChatMessagePushRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT MsgId,MediaId,MsgDataId,IsPreview,MsgStatus,Clientmsgid,Speed,Realspeed,
                                                TotalCount,FilterCount,SentCount,ErrorCount,CheckState,CreateTime,Content
                                            FROM WeChatMessagePushRecord";
        public WeChatMessagePushRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public bool Add(WeChatMessagePushRecordEntity weChatMessagePushRecordEntity)
        {
            string sql = @"INSERT INTO WeChatMessagePushRecord(MsgId,MediaId,MsgDataId,IsPreview,MsgStatus,Clientmsgid,Speed,Realspeed,TotalCount,FilterCount,SentCount,ErrorCount,CheckState,Content)
                                VALUES(?MsgId,?MediaId,?MsgDataId,?IsPreview,?MsgStatus,?Clientmsgid,?Speed,?Realspeed,?TotalCount,?FilterCount,?SentCount,?ErrorCount,?CheckState,?Content);";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MsgId", weChatMessagePushRecordEntity.MsgId, DbType.Int64, size: 20);
            parameters.Add("?MediaId", weChatMessagePushRecordEntity.MediaId, DbType.AnsiString, size: 100);
            parameters.Add("?MsgDataId", weChatMessagePushRecordEntity.MsgDataId, DbType.Int64, size: 20);
            parameters.Add("?IsPreview", weChatMessagePushRecordEntity.IsPreview, DbType.Boolean);
            parameters.Add("?MsgStatus", weChatMessagePushRecordEntity.MsgStatus, DbType.AnsiString, size: 20);
            parameters.Add("?Clientmsgid", weChatMessagePushRecordEntity.Clientmsgid, DbType.AnsiString, size: 64);
            parameters.Add("?Speed", weChatMessagePushRecordEntity.Speed, DbType.Int32, size: 10);
            parameters.Add("?Realspeed", weChatMessagePushRecordEntity.Realspeed, DbType.Int32, size: 10);
            parameters.Add("?TotalCount", weChatMessagePushRecordEntity.TotalCount, DbType.Int32, size: 10);
            parameters.Add("?FilterCount", weChatMessagePushRecordEntity.FilterCount, DbType.Int32, size: 10);
            parameters.Add("?SentCount", weChatMessagePushRecordEntity.SentCount, DbType.Int32, size: 10);
            parameters.Add("?ErrorCount", weChatMessagePushRecordEntity.ErrorCount, DbType.Int32, size: 10);
            parameters.Add("?CheckState", weChatMessagePushRecordEntity.CheckState, DbType.Int64, size: 20);
            parameters.Add("?Content", weChatMessagePushRecordEntity.Content, DbType.AnsiString, size: 600);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }

        public bool UpdateMPStatus(WeChatMessagePushRecordEntity weChatMessagePushRecordEntity)
        {
            string sql = @"UPDATE WeChatMessagePushRecord SET
                                MediaId=?MediaId,
                                MsgDataId=?MsgDataId,
                                IsPreview=?IsPreview,
                                MsgStatus=?MsgStatus,
                                Clientmsgid=?Clientmsgid,
                                Speed=?Speed,
                                Realspeed=?Realspeed,
                                TotalCount=?TotalCount,
                                FilterCount=?FilterCount,
                                SentCount=?SentCount,
                                ErrorCount=?ErrorCount,
                                CheckState=?CheckState,
                                Content=?Content
                            WHERE MsgId=?MsgId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MsgId", weChatMessagePushRecordEntity.MsgId, DbType.Int64, size: 20);
            parameters.Add("?MediaId", weChatMessagePushRecordEntity.MediaId, DbType.AnsiString, size: 100);
            parameters.Add("?MsgDataId", weChatMessagePushRecordEntity.MsgDataId, DbType.Int64, size: 20);
            parameters.Add("?IsPreview", weChatMessagePushRecordEntity.IsPreview, DbType.Boolean);
            parameters.Add("?MsgStatus", weChatMessagePushRecordEntity.MsgStatus, DbType.AnsiString, size: 20);
            parameters.Add("?Clientmsgid", weChatMessagePushRecordEntity.Clientmsgid, DbType.AnsiString, size: 64);
            parameters.Add("?Speed", weChatMessagePushRecordEntity.Speed, DbType.Int32, size: 10);
            parameters.Add("?Realspeed", weChatMessagePushRecordEntity.Realspeed, DbType.Int32, size: 10);
            parameters.Add("?TotalCount", weChatMessagePushRecordEntity.TotalCount, DbType.Int32, size: 10);
            parameters.Add("?FilterCount", weChatMessagePushRecordEntity.FilterCount, DbType.Int32, size: 10);
            parameters.Add("?SentCount", weChatMessagePushRecordEntity.SentCount, DbType.Int32, size: 10);
            parameters.Add("?ErrorCount", weChatMessagePushRecordEntity.ErrorCount, DbType.Int32, size: 10);
            parameters.Add("?CheckState", weChatMessagePushRecordEntity.CheckState, DbType.Int64, size: 20);
            parameters.Add("?Content", weChatMessagePushRecordEntity.Content, DbType.AnsiString, size: 600);
            int result = this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
            return result > 0;
        }

        public WeChatMessagePushRecordEntity GetEntity(long msgId)
        {
            string sql = "{0} WHERE MsgId=?MsgId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MsgId", msgId, DbType.Int64, size: 20);
            return this.databaseContext.Slave.QueryEntity<WeChatMessagePushRecordEntity>(string.Format(sql, QUERY), parameters);
        }

        public WeChatMessagePushRecordEntity GetEntity(string mediaId)
        {
            string sql = "{0} WHERE MediaId=?MediaId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MediaId", mediaId, DbType.AnsiString, size: 100);
            return this.databaseContext.Slave.QueryEntity<WeChatMessagePushRecordEntity>(string.Format(sql, QUERY), parameters);
        }

        public IEnumerable<WeChatMessagePushRecordEntity> GetList(string mediaId, string msgStatus, DateTime? start, DateTime? end, out int dataCount, bool? ascending, int pageIndex = 1, int pageSize = 10)
        {
            DynamicParameters parameters = new DynamicParameters();
            List<string> wheres = new List<string>();
            if (!string.IsNullOrWhiteSpace(mediaId))
            {
                wheres.Add("MediaId=?MediaId");
                parameters.Add("?MediaId", mediaId, DbType.AnsiString, size: 100);
            }
            if (!string.IsNullOrWhiteSpace(msgStatus))
            {
                wheres.Add("MsgStatus=?MsgStatus");
                parameters.Add("?MsgStatus", msgStatus.ToString(), DbType.AnsiString,size:20);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime {4} LIMIT {2},{3}; 
                               SELECT Count(1) FROM WeChatMessagePushRecord {1}",
                QUERY,
                wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                pageSize,
                ascending.HasValue && ascending.Value ? "ASC" : "DESC");

            var result = this.databaseContext.Slave.QueryMultiple<WeChatMessagePushRecordEntity, int>(sql, parameters);

            dataCount = result.Item2;

            return result.Item1;
        }

        public void Delete(long msgId)
        {
            string sql = @"DELETE FROM WeChatMessagePushRecord WHERE MsgId=?MsgId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MsgId", msgId, DbType.Int64, size: 20);
            this.databaseContext.Master.Execute(sql, parameters, databaseContext.Transaction);
        }

       
    }
}
